﻿using System;
using Component;
using Component.Track;
using Component.UI;
using Component.UI.Entity;
using Component.UI.View.PagerView;
using Tools.ScrollBar;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace FScrollPage {

    public class FScrollItem : MonoBehaviour,IFScrollItem , IPointerUpHandler , IPointerDownHandler
    {

        protected internal FScrollPager ScrollPager;

        protected int ItemId;

        private Vector2 lastPointer;
        protected bool selected;

        private void Awake()
        {
            ScrollPager = GetComponentInParent<PagerView>();
        }

        protected virtual void Start()
        {
        }

        public virtual void OnScrollClick()
        {
            if (selected)
            {
                selected = false;
            }
        }

        public virtual void OnScrollPitch()
        {
            selected = true;
        }

        public virtual void OnScrollOver()
        {
            selected = false;
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            //传递事件
            ScrollPager.OnPointerDown(eventData);
            //后续逻辑
            lastPointer = eventData.position;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            ScrollPager.OnPointerUp(eventData);
            if (Vector2.Distance(lastPointer, eventData.position) <= 2f) {
                OnScrollClick();
                ScrollPager.MoveToItemID(ItemId);
            }
        }

        public void OnScrollInit(FScrollPager pager , int itemID)
        {
            ScrollPager = pager;
            ItemId = itemID;
        }
    }
    
    public interface IFScrollItem
    {
        /// <summary>
        /// 初始化
        /// </summary>
        void OnScrollInit(FScrollPager pager , int ItemID);
        ///被点击时
        void OnScrollClick();
        ///被选中时
        void OnScrollPitch();
        ///未选中时
        void OnScrollOver();
    }
}